package com.app.ytong.greendao;


import com.alibaba.fastjson.annotation.JSONField;
import com.app.ytong.bean.GoodsLevelListBean;
import com.app.ytong.bean.GoodsLevelListData;
import com.app.ytong.bean.GoodsLevelListDataDao;
import com.app.ytong.bean.GoodsTimeListBean;
import com.app.ytong.bean.GoodsTimeListData;
import com.app.ytong.bean.GoodsTimeListDataDao;
import com.app.ytong.bean.GoodsTypeListBean;
import com.app.ytong.bean.GoodsTypeListData;
import com.app.ytong.bean.GoodsTypeListDataDao;
import com.app.ytong.bean.TraderListBean;
import com.app.ytong.bean.TraderListData;
import com.app.ytong.bean.TraderListDataDao;
import com.app.ytong.bean.UserListBean;
import com.app.ytong.bean.UnitListBean;
import com.app.ytong.bean.UnitListData;
import com.app.ytong.bean.UnitListDataDao;
import com.app.ytong.bean.UserListData;
import com.app.ytong.bean.UserListDataDao;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class GreenDaoUtils {

    private static GoodsTypeListDataDao goodsTypeDao = GreenDaoManager.getInstance().getmDaoSession().getGoodsTypeListDataDao();
    private static GoodsLevelListDataDao goodsLevelDao = GreenDaoManager.getInstance().getmDaoSession().getGoodsLevelListDataDao();
    private static GoodsTimeListDataDao goodsTimeDao = GreenDaoManager.getInstance().getmDaoSession().getGoodsTimeListDataDao();
    private static UserListDataDao userDataDao = GreenDaoManager.getInstance().getmDaoSession().getUserListDataDao();
    private static UnitListDataDao unitDataDao = GreenDaoManager.getInstance().getmDaoSession().getUnitListDataDao();
    private static TraderListDataDao traderDataDao = GreenDaoManager.getInstance().getmDaoSession().getTraderListDataDao();


    public static final String
            TYPE_GOODS= "goodsType" //货品类型
            , TYPE_LEVEL = "goodsLevel"        //货品等级
            , TYPE_TIME = "goodsTime"        //货品年份
            , TYPE_USER = "user"        //茶农，供应商
            , TYPE_TRADER = "trader"        //供应商
            , TYPE_UNIT = "unit"        //单位
            ;


    public static void clearAll(){
        goodsTypeDao.deleteAll();
        goodsLevelDao.deleteAll();
        goodsTimeDao.deleteAll();
        userDataDao.deleteAll();
        unitDataDao.deleteAll();
        traderDataDao.deleteAll();
        EventBus.getDefault().post(new GreenDaoUtils.EventDateChange(EventDateChange.TYPE_ALL));
    }


    public static void traderypeInsertData(TraderListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        traderDataDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void goodsTypeInsertData(GoodsTypeListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        goodsTypeDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void goodsLevelInsertData(GoodsLevelListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        goodsLevelDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void goodsTimeInsertData(GoodsTimeListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        goodsTimeDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void userBeanInsertData(UserListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        userDataDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void unitInsertData(UnitListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        unitDataDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static String  checkIsTraderOrSupplier(String no){
        List<TraderListData> userList = traderDataDao.queryBuilder().where(TraderListDataDao.Properties.Cdno.eq(no)).build().list();
        if(userList.size()>0){
            return "trader";
        }
        return "";
    }


    /**
     * 查找
     **/

    public static GoodsTypeListBean getGoodsTypeListBean() {
        List<GoodsTypeListData> list = goodsTypeDao.queryBuilder().build().list();
        GoodsTypeListBean listBean = new GoodsTypeListBean();
        for (GoodsTypeListData data : list){
            listBean.getList().add(data);
        }
        return listBean;
    }

    public static GoodsLevelListBean getGoodsLevelListBean() {
        List<GoodsLevelListData> list = goodsLevelDao.queryBuilder().build().list();
        GoodsLevelListBean listBean = new GoodsLevelListBean();
        for (GoodsLevelListData data : list){
            listBean.getList().add(data);
        }
        return listBean;
    }

    public static GoodsTimeListBean getGoodsTimeListBean() {
        List<GoodsTimeListData> list = goodsTimeDao.queryBuilder().build().list();
        GoodsTimeListBean listBean = new GoodsTimeListBean();
        for (GoodsTimeListData data : list){
            listBean.getList().add(data);
        }
        return listBean;
    }


    public static UserListBean getUserListBean() {
        List<UserListData> list = userDataDao.queryBuilder().build().list();
        UserListBean userListBean = new UserListBean();
        for (UserListData data : list){
            userListBean.getList().add(data);
        }
        return userListBean;
    }

    public static UnitListBean getUnitListBean() {
        List<UnitListData> list = unitDataDao.queryBuilder().build().list();
        UnitListBean listBean = new UnitListBean();
        for (UnitListData data : list){
            listBean.getList().add(data);
        }
        return listBean;
    }

    public static TraderListBean getTraderListBean() {
        List<TraderListData> list = traderDataDao.queryBuilder().build().list();
        TraderListBean listBean = new TraderListBean();
        for (TraderListData data : list){
            listBean.getList().add(data);
        }
        return listBean;
    }


    public static GoodsTypeListBean getGoodsTypeList() {
        List<GoodsTypeListData> list = goodsTypeDao.queryBuilder().build().list();
        GoodsTypeListBean listBean = new GoodsTypeListBean();
        listBean.setList((ArrayList<GoodsTypeListData>) list);
        return listBean;
    }

    public static GoodsTypeListBean getGoodsTypeListBeanAndInit() {
        List<GoodsTypeListData> list = goodsTypeDao.queryBuilder().build().list();
        GoodsTypeListBean listBean = new GoodsTypeListBean();
        GoodsTypeListData initData = new GoodsTypeListData();
        initData.setTid("");
        initData.setTna("全部");
        listBean.getList().add(initData);
        for (GoodsTypeListData data : list){
            listBean.getList().add(data);
        }
        return listBean;
    }



    public static class EventDateChange {
        public static final String TYPE_ALL = "99";
        public String type;

        public EventDateChange(String type) {
            this.type = type;
        }
    }

    public interface GreenDaoInserCallBack {
        void onDataInsert(int count);
    }

    @JSONField(serialize = false)
    public static String getTypeName(String map_type) {
        switch (map_type) {
            case GreenDaoUtils.TYPE_GOODS:
                return "货品资料";
            case GreenDaoUtils.TYPE_USER:
                return "用户";
            default:
                return "未知";
        }
    }
}
